package com.maple.p2pclient.utils;

import android.content.Context;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

public class PermissionUtils {
    public static void requestCamera(Context context, RequestPermission result){
        List<String> permissionList = new ArrayList<>();
        //相机
        permissionList.add(Permission.CAMERA);
        permissionList.add(Permission.RECORD_AUDIO);
        permissionList.add(Permission.READ_PHONE_STATE);

        XXPermissions.with(context).permission(permissionList).request(new OnPermissionCallback() {
            @Override
            public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                if (allGranted) {
                    result.onRequestPermissionSuccess();
                } else {
                    result.onRequestPermissionFailure(context,permissions);
                }
            }

            @Override
            public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
                if (doNotAskAgain) {
                    result.onRequestPermissionFailureWithAskNeverAgain(context,permissions);
                } else {
                    result.onRequestPermissionFailure(context,permissions);
                }
            }
        });
    }

    public interface RequestPermission {
        void onRequestPermissionSuccess();
        void onRequestPermissionFailure(Context context, List<String> permissions);
        void onRequestPermissionFailureWithAskNeverAgain(Context context, List<String> permissions);
    }
}
